// import { isArray } from '@/utils'
import { setToken, removeToken } from '@/utils/auth'

const user = {
  state: {
    loginStatus: 0, // 是否登录
    token: '',
    nickName: '游客',
    authorization: 999, // 授权剩余时间
    qqMapKey: '' // 腾讯地图key
  },
  mutations: {
    SET_USER_TOKEN: (state, token) => {
      setToken(token)
      state.token = token
    },
    INIT_USER: (state, arg) => {
      console.log('asdf')
      state.token = arg.token
      state.loginStatus = 1
      state.nickName = arg.nickname
      state.qqMapKey = arg.qqmapkey
      state.authorization = arg.authorization
      setToken(arg.token)
      sessionStorage.setItem('state', state)
    },
    LOGOUT: (state) => {
      removeToken()
      state.token = ''
      state.loginStatus = 0
      state.nickName = '游客'
      state.authorization = 0
      state.qqMapKey = ''
    }
  },
  actions: {
    SetUserToken ({ commit }, token) {
      commit('SET_USER_TOKEN', token)
    },
    InitUser ({ commit }, arg) {
      return new Promise((resolve, reject) => {
        commit('INIT_USER', arg)
        resolve()
      })
    },
    Logout ({ commit }) {
      commit('LOGOUT')
    }
  }
}

export default user
